//
//  ViewController.m
//  Multimedia
//
//  Created by 广州智能 on 16/2/26.
//  Copyright © 2016年 广州智能. All rights reserved.
//

#import "ViewController.h"
#import "GetFilePath.h"

@interface ViewController ()<UINavigationBarDelegate,UIImagePickerControllerDelegate >
@property (nonatomic,strong) UIButton *pzbtn;
@property (nonatomic,strong) UIButton *sybtn;;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *pzbtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    [pzbtn setTitle:@"拍照" forState:UIControlStateNormal];
    
    pzbtn.backgroundColor = [UIColor purpleColor];
    
    [pzbtn addTarget:self action:@selector(paizhao:) forControlEvents:UIControlEventTouchDown];
    
   
    
    UIButton *sybtn = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 200, 200)];
    
    [sybtn setTitle:@"摄影" forState:UIControlStateNormal];
    
    sybtn.backgroundColor = [UIColor yellowColor];
    
    [sybtn addTarget:self action:@selector(sheying:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:pzbtn];
    [self.view addSubview:sybtn];
    
}

//拍照
-(void)paizhao:(UIButton *)pzbtn
{
    UIImagePickerController *picker = [[UIImagePickerController  alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;//可编辑
    //判断是否可以打开相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //摄像头
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
    }else{//否则打开图片库
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:picker animated:YES completion:nil];
}
//摄影
-(void)sheying:(UIButton *)sheying{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        picker.videoQuality = UIImagePickerControllerQualityTypeMedium; //录像质量
        picker.videoMaximumDuration = 600.0f; //录像最长时间
        picker.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"当前设备不支持录像功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
    //跳转到拍摄页面
    [self presentViewController:picker animated:YES completion:nil];
}

//拍摄完成后要执行的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([mediaType isEqualToString:@"public.image"]) {
        //得到照片
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //图片存入相册
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }
        
        NSString *imagePath = [GetFilePath getSavePathWithFileSuffix:@"png"];
        success = [fileManager fileExistsAtPath:imagePath];
        if (success) {
            [fileManager removeItemAtPath:imagePath error:nil];
        }
        
        NSData *imageData = UIImagePNGRepresentation(image);
        [imageData writeToFile:imagePath atomically:YES]; //写入本地
        success = [fileManager fileExistsAtPath:imagePath];
        if (success) {
            NSLog(@"图片写入成功,image路径:%@",imagePath);
        }
    } else if ([mediaType isEqualToString:@"public.movie"]) {
        NSString *videoPath = [GetFilePath getSavePathWithFileSuffix:@"mov"];
        success = [fileManager fileExistsAtPath:videoPath];
        if (success) {
            [fileManager removeItemAtPath:videoPath error:nil];
        }
        
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSData *videlData = [NSData dataWithContentsOfURL:videoURL];
        [videlData writeToFile:videoPath atomically:YES]; //写入本地
        //存储数据
        success = [fileManager fileExistsAtPath:videoPath];
        if (success) {
            NSLog(@"media 写入成功,video路径:%@",videoPath);
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

//进入拍摄页面点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
